{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "de8446f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从sklearn.datasets 里导入手写体数字加载器\n",
    "from sklearn.datasets import load_digits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1579510f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#从通过数据加载器获得手写数字的数码图像数据并存储在digits中\n",
    "digits = load_digits()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "86922317",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797, 64)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检视数据规模和数据纬度\n",
    "digits.data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "818ab911",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 从sklearn.cross_validation中导入train_test_split用于数据分割\n",
    "from sklearn.model_selection import  train_test_split\n",
    "\n",
    "X_train,X_test,y_train,y_test = train_test_split(digits.data,digits.target,test_size=0.25,random_state=33)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a7b1fec2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1347,)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分别检视训练与测试数据规模\n",
    "y_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6a6b0ae7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(450,)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "365f0f84",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用支持向量机对手写数字图像进行识别\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "# 对测试集和训练集进行标准化\n",
    "ss = StandardScaler()\n",
    "X_train = ss.fit_transform(X_train)\n",
    "X_test = ss.transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "82a30a17",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/jovyan/.virtualenvs/basenv/lib/python3.7/site-packages/sklearn/svm/_base.py:947: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.\n",
      "  \"the number of iterations.\", ConvergenceWarning)\n"
     ]
    }
   ],
   "source": [
    "# 从sklearn.svm中导入基于现行假设的支持向量机分类器LinearSVC \n",
    "from sklearn.svm import LinearSVC\n",
    "\n",
    "# 初始化现行假设的支持向量机分类器 LinearSVC\n",
    "lsvc = LinearSVC()\n",
    "\n",
    "# 进行模型训练\n",
    "lsvc.fit(X_train,y_train)\n",
    "\n",
    "y_predict = lsvc.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ab2e04a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Accuracy of Linear SVC is 0.953333\n"
     ]
    }
   ],
   "source": [
    "# 使用自带的模型评估函数进行准确性评测\n",
    "print ('The Accuracy of Linear SVC is %f'%lsvc.score(X_test,y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "74b51ac3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.92      1.00      0.96        35\n",
      "           1       0.96      0.98      0.97        54\n",
      "           2       0.98      1.00      0.99        44\n",
      "           3       0.93      0.93      0.93        46\n",
      "           4       0.97      1.00      0.99        35\n",
      "           5       0.94      0.94      0.94        48\n",
      "           6       0.96      0.98      0.97        51\n",
      "           7       0.92      1.00      0.96        35\n",
      "           8       0.98      0.84      0.91        58\n",
      "           9       0.95      0.91      0.93        44\n",
      "\n",
      "    accuracy                           0.95       450\n",
      "   macro avg       0.95      0.96      0.95       450\n",
      "weighted avg       0.95      0.95      0.95       450\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 使用sklearn.metrics里面的classification_report模块对预测结果做更加详细的分析\n",
    "from sklearn.metrics import  classification_report\n",
    "print(classification_report(y_test,y_predict,target_names=digits.target_names.astype(str)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd7c9a80",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
